/*Summary-----------------------------------------------------------------------

------------------------------------------------------------------------------*/

*Parameters--------------------------------------------------------------------
local exch=2288 //Exchange rate TSH per USD

*get invoice value and area for 225 sampled properties--------------------------
use "${sdir}/Sample_owners.dta", clear
keep ShinaID plot_code full_cost invoice lot_area_inv 
rename (ShinaID plot_code) (shina_id plot_id)
save "temp/owner_sample_full_cost.dta", replace


*get list of leader predicitons-------------------------------------------------
use "${sdir}/Leaders_and_Owners_cln.dta", clear
keep shina_id plot_id q42_ldr
gen WTP=q42_ldr
collapse (mean) WTP, by(shina_id plot_id)
rename WTP WTP_ldr
save "temp/leader_WTP_predictions.dta", replace

*get 146 that actually attended-------------------------------------------------
use "${sdir}/Owners_cln.dta", clear
keep shina_id plot_id owner_observed empty_lot_pltpht construction_pltpht gated_pltpht gate_style_pltpht walls_pltpht roof_pltpht floors_pltpht use_pltpht rank_value_pltpht value_pltpht

merge 1:1 shina_id plot_id using "temp/owner_sample_full_cost.dta", nogen
merge 1:1 shina_id plot_id using "temp/leader_WTP_predictions.dta", nogen
merge 1:1 shina_id plot_id using "${sdir}/owner_invited_list.dta", nogen
order shina_id plot_id owner_observed invited

*run attrition checks-----------------------------------------------------------
*adjust variables
replace gated_pltpht=0 if empty_==1 //if plot is empty, set gated to none
gen goodwall=(wall==3)
replace WTP_ldr=WTP_ldr/`exch'
replace full_cost =full_cost /`exch'
replace value_pltpht =value_pltpht /`exch'
gen attrited=(owner_observed==0)
*gen attrited=(owner_observed==0 & invited==1)
*gen not_invited=(invited==0)

*labels
label var owner_observed 	"Attended"
label var attrited 			"Attrited"
*label var not_invited 		"Not Invited"
label var lot_area_inv 		"\makecell{Lot Size\\(sq-m)}"
label var gated_pltpht 		"\makecell{Gated}"
label var goodwall 			"\makecell{Plaster\\Wall}"
label var full_cost 		"\makecell{Invoice\\Value (USD)}"
label var value_pltpht 		"\makecell{Estimated\\Value}"
label var WTP_ldr 			"\makecell{Leader\\WTP\\Prediction}"
save "temp/attrited_plots.dta", replace

*Characteristics by attrition---------------------------------------------------
local reg_lst lot_area_inv gated_pltpht goodwall full_cost /*value_pltpht WTP_ldr */
local c=0
foreach v in `reg_lst'{
    local c=`c'+1
	*Panel A: Full Sample
    eststo pnl1_`c': reg `v' attrited, vce(robust)
	*Panel B: Invited Sample
    eststo pnl2_`c': reg `v' attrited if invited==1, vce(robust)
	}

*write to file
local note ///
	This table presents coefficients from regressions of plot characteristics on a dummy for the plot being included in our survey. Each observation is a plot. The full sample is the 225 randomly sampled plots and the invited sample is the 209 plots that were eligible for the survey. Robust standard errors in parentheses. \sym{*}\(p<0.10\),	\sym{**}\(p<0.05\), \sym{***}\(p<0.01\).
		
*put all panels together in one table
//top panel 
esttab pnl1* using "tables/attrition_check.tex", ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	se label wrap fragment ///
	prehead("\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} {\centering\begin{tabular}{l*{5}{c}} \hline\hline") ///
	posthead("\hline \\ \multicolumn{4}{l}{\textit{Panel A: Full sample of plots}} \\\\[-1ex]") ///
	replace 
*eststo clear

//bottom panel 
esttab pnl2* using "tables/attrition_check.tex", ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	se label wrap fragment ///
	nomtitles nonumbers ///
	posthead("\hline \\ \multicolumn{4}{l}{\textit{Panel B: Invited sample of plots}} \\\\[-1ex]") ///
	prefoot("\hline") ///
	postfoot("\hline\hline \end{tabular}\par\medskip}{\footnotesize {\it Note: }`note'}") ///
	append
estimates clear

	
	


*END--------
clear
